package com.club.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.club.common.R;
import com.club.entity.ClubMember;
import com.club.service.ClubMemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 社团成员管理控制器
 */
@RestController
@RequestMapping("/club-member")
@Tag(name = "社团成员管理", description = "社团成员相关接口")
public class ClubMemberController {

    @Autowired
    private ClubMemberService clubMemberService;

    /**
     * 添加社团成员
     */
    @SaCheckLogin
    @PostMapping("/add")
    @Operation(summary = "添加成员", description = "添加新成员到社团")
    public R addMember(@RequestBody ClubMember clubMember) {
        boolean success = clubMemberService.addMember(clubMember);
        return success ? R.ok().message("添加成功") : R.error().message("添加失败");
    }

    /**
     * 移除社团成员
     */
    @SaCheckLogin
    @DeleteMapping("/remove/{clubId}/{userId}")
    @Operation(summary = "移除成员", description = "从社团移除指定成员")
    public R removeMember(@PathVariable("clubId") Long clubId, @PathVariable("userId") Long userId) {
        boolean success = clubMemberService.removeMember(clubId, userId);
        return success ? R.ok().message("移除成功") : R.error().message("移除失败");
    }

    /**
     * 更新成员角色
     */
    @SaCheckLogin
    @PutMapping("/update-role/{clubId}/{userId}/{role}")
    @Operation(summary = "更新角色", description = "更新成员在俱乐部中的角色")
    public R updateMemberRole(@PathVariable("clubId") Long clubId, @PathVariable("userId") Long userId, @PathVariable("role") Integer role) {
        boolean success = clubMemberService.updateMemberRole(clubId, userId, role);
        return success ? R.ok().message("角色更新成功") : R.error().message("角色更新失败");
    }

    /**
     * 查询社团成员列表
     */
    @SaCheckLogin
    @GetMapping("/list/{clubId}")
    @Operation(summary = "成员列表", description = "查询指定社团的所有成员")
    public R getMembersByClubId(@PathVariable("clubId") Long clubId) {
        List<ClubMember> members = clubMemberService.getMembersByClubId(clubId);
        return R.ok().data(members);
    }

    /**
     * 查询用户在社团中的角色
     */
    @SaCheckLogin
    @GetMapping("/check/{clubId}/{userId}")
    @Operation(summary = "检查成员角色", description = "查询用户在指定社团中的角色信息")
    public R getMemberByClubAndUser(@PathVariable("clubId") Long clubId, @PathVariable("userId") Long userId) {
        ClubMember member = clubMemberService.getMemberByClubAndUser(clubId, userId);
        return member != null ? R.ok().data(member) : R.error().message("用户不是该社团成员");
    }

    /**
     * 检查用户是否是社团成员
     */
    @SaCheckLogin
    @GetMapping("/is-member/{clubId}/{userId}")
    @Operation(summary = "检查成员资格", description = "检查用户是否是指定社团的成员")
    public R checkIsMember(@PathVariable("clubId") Long clubId, @PathVariable("userId") Long userId) {
        boolean isMember = clubMemberService.isMember(clubId, userId);
        return R.ok().data(isMember);
    }

    /**
     * 检查用户是否是社团管理员
     */
    @SaCheckLogin
    @GetMapping("/is-admin/{clubId}/{userId}")
    @Operation(summary = "检查管理员权限", description = "检查用户是否是指定社团的管理员")
    public R checkIsAdmin(@PathVariable("clubId") Long clubId, @PathVariable("userId") Long userId) {
        boolean isAdmin = clubMemberService.isAdmin(clubId, userId);
        return R.ok().data(isAdmin);
    }
}